class Solution {
public:
    int countSubstrings(string s) {
        int n = s.size();
        vector<vector<bool>>f(n, vector<bool>(n));
        int ans = 0;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (s[i] == s[j]) {
                    if (i == j || i + 1 == j)f[i][j] = 1;
                    if (i + 1 < j)f[i][j] = f[i + 1][j - 1];
                }
                if (f[i][j])ans++;
            }
        }
        return ans;
    }
};